package cn.sxt.head.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sxt.common.controller.BaseController;
import cn.sxt.common.util.ConstatFinalUtil;
import cn.sxt.common.util.PageInfoUtil;
import cn.sxt.questions.pojo.Columns;
import cn.sxt.questions.pojo.Items;
import cn.sxt.questions.service.IQuestionService;
import cn.sxt.users.pojo.POJOEnum;

@Controller
public class HeadController extends BaseController{
	@Resource
	private IQuestionService questionService;
	
	@RequestMapping("/head/main")
	public String main(){
		return "head/main";
	}
	
	@RequestMapping("/head")
	public String head(HttpServletRequest request){
		/*查询栏目*/
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.put("status", POJOEnum.STATUS_ENABLE.getStatus());
		List<Columns> columnsList=this.questionService.findListColumnsService(condMap, null);
		request.setAttribute("columnsList", columnsList);
		return "/head/header";
	}
	/**
	 * 这里是分页查询测试项的信息
	 * @param request
	 * @param url
	 * @param page
	 * @return
	 */
	@RequestMapping("/{url}")
	public String itemsList(HttpServletRequest request,@PathVariable String url,Integer currentPage){
		System.out.println("---itemsList----");
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.put("url", url);
		Columns columns=this.questionService.findOneColumnsService(condMap);
		/*这里对分页信息进行处理*/
		PageInfoUtil pageInfoUtil=new PageInfoUtil();
		if(currentPage!=null){
			pageInfoUtil.setCurrentPage(currentPage);
		}
		condMap.clear();
		condMap.put("columnsId", columns.getId());
		List<Items> itemsList=this.questionService.findListItemsService(condMap, pageInfoUtil);
		request.setAttribute("itemsList", itemsList);
		request.setAttribute("columns", columns);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		return "/head/itemsList";
	}
	
	@RequestMapping("/head/{id}")
	public String itemsInfo(HttpServletRequest request,@PathVariable Integer id){
		/*传过来的是 Items的id*/
		System.out.println("---itemsInfo---");
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.put("id", id);
		/*对Items信息进行查询*/
		Items items=this.questionService.findOneItemsService(condMap);
		request.setAttribute("items", items);
		/*接下来去查询上一条和下一条*/
		condMap.clear();
		/*查询上一条*/
		condMap.put("pubTimeDa", items.getPubTime());
		condMap.put("orderby","pubTimeASC");
		/*只需要获得第一个即可*/
		PageInfoUtil pageInfoUtil=new PageInfoUtil();
		pageInfoUtil.setPageSize(1);
		List<Items> itemsList1=this.questionService.findListItemsService(condMap, pageInfoUtil);
		request.setAttribute("preItems", itemsList1.get(0));
		/*查询下一个即可*/
		condMap.clear();
		condMap.put("pubTimeXiao", items.getPubTime());
		List<Items> itemsList2=this.questionService.findListItemsService(condMap, pageInfoUtil);
		request.setAttribute("nextItems", itemsList2.get(0));
		return "/head/itemsInfo";
	}
	
	@RequestMapping("/head/{id}/start")
	public String question(HttpServletRequest request,@PathVariable Integer id){
		/*查询所有的问题与答案*/
		ConstatFinalUtil.LOGGER.info("---question---");
		Map<String,Object> condMap=new HashMap<String,Object>();
		condMap.clear();
		condMap.put("id", id);
		Items items=this.questionService.findOneItemsService(condMap);
		request.setAttribute("items", items);
		return "/head/question";
	}
}
